package com.yy.controller;

import com.yy.Utils.AjaxResult;
import com.yy.Utils.CurPool;
import com.yy.Utils.JsonResult;
import com.yy.mapper.UserMapper;
import com.yy.pojo.Power;
import com.yy.pojo.Role;
import com.yy.pojo.User;
import com.yy.pojo.UserRole;
import com.yy.service.RoleService;
import com.yy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author YY
 */
@Controller
@RestController
@RequestMapping("user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;
    @Resource
    private RoleService roleService;

    /**
     * 根据id选择用户
     */
    @GetMapping("/getUserByid")
    public String getUserByid(){
        User user = userService.getUserByid("001");
        System.out.println(user);
        return "ok";
    }

    @RequestMapping("adduser")
    public JsonResult<Void> addUser(User user, UserRole userRole) {
        userService.addUser(user,userRole);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("addRole")
    public JsonResult<Void> addRole(UserRole userRole){
        userService.addRole(userRole);
        return new JsonResult<>(OK);
    }

    @RequestMapping("login")
    public JsonResult<User> login(String username, String password, HttpSession session) {
        User data = userService.login(username, password);
        session.setAttribute("uid", data.getUid());
        session.setAttribute("username", data.getUname());
        session.setAttribute("urname",data.getUrname());
        return new JsonResult<User>(OK, data);
    }

    @GetMapping("get_by_uid")
    public JsonResult<User> getByUid(HttpSession session) {
        String uid = getUidFromSession(session);
        User data = userService.getByUid(uid);
        return new JsonResult<User>(OK, data);
    }

    @GetMapping("get_by_uname")
    public JsonResult<User> getByUname(HttpSession session) {
        String uname = getUidFromSession(session);
        User data = userService.getByUid(uname);
        return new JsonResult<User>(OK, data);
    }

    @RequestMapping("{uname}/delete")
    public JsonResult<Void> delete(@PathVariable("uname") String uname){
        userService.deleteUser(uname);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("get_user_list")
    public JsonResult<List<User>> getUserList(){
        List<User> data = userService.getUserList();
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("getRole")
    public JsonResult<List<User>> getRole(){
        List<User> data = userService.getRole();
        return new JsonResult<>(OK,data);
    }
}
